package com.lfc.myframe.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.widget.RadioButton;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.tamsiree.rxkit.RxImageTool;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by LFC
 * on 2020/5/15.
 */
public class RadioButtonUtils {

    /**
     * 设置底部导航 RadioButton 的selector
     *
     * @param radioButton
     * @param normalUrl   默认图标URl
     * @param checkedUrl  选中图标URL
     */
    public void setRadioButtonSelectorDrawable(Context ctx, RadioButton radioButton, String normalUrl, String checkedUrl) {

        StateListDrawable stateListDrawable = new StateListDrawable();

        Observable.create(new ObservableOnSubscribe<Drawable>() {
            @Override
            public void subscribe(ObservableEmitter<Drawable> emitter) throws Exception {
                //先加载默认样式
                Glide.with(ctx)
                        .load(normalUrl)
                        .into(new SimpleTarget<Drawable>() {
                            @Override
                            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                                emitter.onNext(resource);
                                emitter.onComplete();
                            }
                        });

            }
        }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Drawable>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Drawable normalDrawable) {
                        stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, normalDrawable);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {
                        //再加载选中的样式
                        Glide.with(ctx)
                                .load(checkedUrl)
                                .into(new SimpleTarget<Drawable>() {
                                    @Override
                                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                                        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, resource);
                                        //给RadioButton设置selector   对应xml布局里的android:drawableTop 属性
                                        stateListDrawable.setBounds(
                                                0,
                                                0,
                                                RxImageTool.dip2px(ctx, 22f),
                                                RxImageTool.dip2px(ctx, 22f)
                                        );
//                                        ((MyRadioButton) radioButton).setmDrawableSize(RxImageTool.dip2px(ctx, 50f));
                                        radioButton.setCompoundDrawablesWithIntrinsicBounds(null, stateListDrawable, null, null);
                                    }
                                });
                    }
                });


    }

}
